from typing import List
class UnionFind:
    def __init__(self, n):
        self.parent_or_size = [-1] * n

    def find(self, a):
        a = self.parent_or_size[a] if self.parent_or_size[a] >= 0 else a
        acopy = a
        while self.parent_or_size[a] >= 0:
            a = self.parent_or_size[a]
        while acopy != a:
            self.parent_or_size[acopy], acopy = a, self.parent_or_size[acopy]
        return a

    def merge(self, a, b):
        pa, pb = self.find(a), self.find(b)
        if pa == pb: return False
        if self.parent_or_size[pa] > self.parent_or_size[pb]:
            pa, pb = pb, pa
        self.parent_or_size[pa] += self.parent_or_size[pb]
        self.parent_or_size[pb] = pa
        return True

    def getSize(self, a):
        return -self.parent_or_size[self.find(a)]
class Solution:
    def countComponents(self, nums: List[int], x: int) -> int:
        tmp = [[] for _ in range(x + 1)]
        for i, num in enumerate(nums):
            for j in range(num, x + 1, num):
                tmp[j].append(i)
        n = len(nums)
        dsu = UnionFind(n)
        for v in tmp:
            for i in range(1, len(v)):
                dsu.merge(v[i - 1], v[i])
        return len({dsu.find(i) for i in range(n)})

if __name__ == '__main__':
    print("hi")